package gson.emlog.adapter;

import gson.emlog.R;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CloudBlogsAdapter extends BaseAdapter {
	private JSONArray items;
	private Context mContext;
	
	
	
	public CloudBlogsAdapter(JSONArray data, Context mContext) {
		super();
		this.items = data;
		this.mContext = mContext;
	}

	public int getCount() {
		return items.size();
	}

	public JSONObject getItem(int position) {
		try {
			return (JSONObject) items.get(position);
		} catch (Exception e) {
			return null;
		}
	}
	
	public String getUrl(int position) {
		JSONObject obj = this.getItem(position);
		if(obj != null){
			try {
				return obj.get("viewurl").toString();
			} catch (Exception e) {
				return null;
			}
		}
		return null;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		//获取layout实例对象
		LayoutInflater lif = LayoutInflater.from(mContext);
		LinearLayout item = (LinearLayout) lif.inflate(R.layout.cloud_blogs_item, null);
		
		
		try {
			if(item != null){
				JSONObject data = (JSONObject) items.get(position);
				//赋值
				TextView  from = (TextView) item.findViewById(R.id.cloud_blog_from);
				from.setText(data.get("title").toString());
				
				TextView  title = (TextView) item.findViewById(R.id.cloud_blog_title);
				title.setText(data.get("excerpt").toString());

				TextView  digg = (TextView) item.findViewById(R.id.cloud_blog_digg_count);
				digg.setText("("+data.get("digg")+")喜欢");
				
				TextView  views = (TextView) item.findViewById(R.id.cloud_blog_views);
				views.setText("("+data.get("views")+")阅读");
				return item;
			}
		} catch (Exception e) {
			return convertView;
		}
		
		return convertView;
	}

}
